/**
 *	计划任务
 *	开发者：雨中磐石
 *	地址：www.rockoa.com
 *	日期：2023-07-25
 */

package com.framework.Model;

import com.framerock.db.DB;
import com.framerock.db.DBConn;
import com.framerock.db.DBWhere;
import com.framerock.baselib.CDate;
import com.framework.ACore.Model;
import com.framerock.baselib.CallBack;
import com.framerock.baselib.Rock;

import java.util.ArrayList;
import java.util.Map;

public class ModelReim extends Model {

    public ModelReim(DBConn dbs) {
        super(dbs);
        this.setTable("imhistory");
    }

    public static ModelReim get(DBConn dbs)
    {
        return new ModelReim(dbs);
    }

    /**
     * 加到历史记录
     * */
    public void addHistory(Map<String,String> da)
    {
        String type     = da.get("type");
        String receid   = da.get("receid");
        String sendid   = da.get("sendid");
        String where = "`type`="+type+" AND `receid`="+receid+" AND `sendid`="+sendid+"";
        Map<String,String> rs = this.getOne(where);
        if(rs==null){
            if(!da.containsKey("stotal"))da.put("stotal", "1");
            this.insertbo(da);
        }else{
            if(!da.containsKey("stotal"))da.put("stotal", ""+(Rock.getMapint(rs, "stotal")+1));
            this.update(da, where);
        }
    }

    /**
     * 标识已读
     * */
    public void yiduHistory(int type, String receid, int sendid)
    {
        this.update("stotal=0", new DBWhere()
                .winto("type", type)
                .wint("receid", receid)
                .wint("sendid", sendid)
                .show()
        );
    }

    /**
     * 删除历史记录
     * */
    public void delHistory(int type, String receid, int sendid)
    {
        this.delete(new DBWhere()
                .winto("type", type)
                .wint("receid", receid)
                .wint("sendid", sendid)
                .show()
        );
    }

    /**
     * 读取历史记录
     * */
    public ArrayList<Map<String, String>> getHistory(String lastdt)
    {
        String cid   = mRequest.COMPANYID;
        String where = "`cid`="+cid+" AND `type` IN(0,1,4) AND `receid`="+mRequest.USEAID+"";
        if(!Rock.isEmpt(lastdt))where+=" AND `optdt`>'"+lastdt+"'";
        ArrayList<Map<String, String>> rows = this.getAll(where, "type,receid,sendid,lastcont,optdt,stotal,title,lastid", "`optdt` DESC");
        Map<String, String> rs,srs;
        int type;
        String sendid,sendname,sendface;
        ModelCompany cModel = ModelCompany.get(this.nDb);
        ModelFlowunit uModel= null;
        if(rows != null)for(int i=0; i<rows.size(); i++){
            rs      = rows.get(i);
            type    = Rock.getMapint(rs, "type");
            sendid   = rs.get("sendid");
            sendname = "-";
            sendface = "images/noface.png";
            if(type == 0) {
                srs = cModel.companyUsero(cid, sendid);
                if(srs != null){
                    sendname = srs.get("name");
                    sendface = srs.get("face");
                    rs.put("deptname",srs.get("deptname"));
                    rs.put("ranking",srs.get("ranking"));
                }
            }

            if(type == 1){
                srs = groupInfo(Integer.parseInt(sendid));
                if(srs != null){
                    sendname = srs.get("name");
                    sendface = srs.get("face");
                    rs.put("ushu",srs.get("ushu"));
                    rs.put("bqname",srs.get("bqname"));
                    rs.put("bqcolor",srs.get("bqcolor"));
                }
            }

            //单位应用
            if(type == 4) {
                if(uModel == null)uModel = ModelFlowunit.get(this.nDb);
                srs = uModel.getXinxi(sendid);
                if(srs != null){
                    sendname = srs.get("name");
                    sendface = srs.get("icons");
                }
            }

            rs.put("sendname",sendname);
            rs.put("sendface",sendface);
        }
        ArrayList<Map<String, String>> data = getCompanyHis(cid,lastdt);
        for(int i=0;i<data.size();i++)rows.add(data.get(i));

        return rows;
    }

    /**
     * 单位未读
     * */
    public ArrayList<Map<String, String>> getCompanyHis(String cid, String lastdt)
    {
        String swe = DB.dbSql(DB.chuliPerfix("usera"), "id", "`uid`="+mRequest.USERID+"");
        String sql = "SELECT SUM(`stotal`)AS stotal,`cid`,`lastcont`,max(optdt)AS optdt  FROM `"+DB.chuliPerfix("imhistory")+"` WHERE `receid` IN("+swe+") AND `type` IN(0,1,4) AND `cid` NOT IN(0,"+cid+") AND `stotal`>0";
        if(!Rock.isEmpt(lastdt))sql+=" AND `optdt`>'"+lastdt+"'";
        sql += " GROUP BY `cid`";
        ArrayList<Map<String, String>> rows = this.nDb.getAll(sql),data = Rock.getMapList();
        Map<String, String> rs,da,ors;
        String sendname,sendface;
        ModelCompany mobj = ModelCompany.get(this.nDb);
        if(rows != null)for(int i=0;i<rows.size();i++){
            rs = rows.get(i);
            da = Rock.getMap(new String[]{
                 "type","9",
                 "sendid", rs.get("cid"),
                 "receid","0",
                 "lastcont",rs.get("lastcont"),
                 "optdt",rs.get("optdt"),
                 "stotal",rs.get("stotal"),
                 "num",""
            });
            sendname = "-";
            sendface = "images/nologo.png";
            ors = mobj.getXinxi(rs.get("cid"));
            if(ors != null){
                sendname = ors.get("name");
                sendface = ors.get("logo");
                da.put("num",ors.get("num"));
            }

            da.put("sendname",sendname);
            da.put("sendface",sendface);
            data.add(da);
        }
        return  data;
    }

    /**
     * 保存人员群组
     * */
    public void saveGroupUser(int type, int gid, String sids, String cid)
    {
        String optdt = CDate.now(),tabs = "usera";
        String bwher = "`type`="+type+" AND `gid`="+gid+"";
        if(type==0)tabs = "users";
        String sql      = "INSERT INTO `"+ DB.chuliPerfix("imgroupuser")+"` (`type`,`gid`,`sid`,`cid`,`optdt`) ";
        String where    = DB.dbSql(DB.chuliPerfix("imgroupuser"), "sid", bwher);
        String isql     = DB.dbSql(DB.chuliPerfix(tabs), ""+type+","+gid+", `id`, "+cid+", '"+optdt+"'", "`id` IN("+sids+") AND `id` NOT IN("+where+")");
        sql += isql;
        this.nDb.Query(sql);

        int ushu    = this.nDb.getCount(DB.chuliPerfix("imgroupuser"), bwher);
        this.nDb.update(DB.chuliPerfix("imgroup"), "`ushu`="+ushu+"", "`id`="+gid+"");
    }

    /**
     * 退出群
     * */
    public void exitGroup(int type, int gid, String sid)
    {
        String bwher = "`type`="+type+" AND `gid`="+gid+"";
        this.nDb.delete(DB.chuliPerfix("imgroupuser"), ""+bwher+" AND `sid`="+sid+"");
        int ushu    = this.nDb.getCount(DB.chuliPerfix("imgroupuser"), bwher);
        this.nDb.update(DB.chuliPerfix("imgroup"), "`ushu`="+ushu+"", "`id`="+gid+"");
    }

    /**
     * 获取群组
     * */
    public ArrayList<Map<String, String>> getGroupArr(String aid, String uid)
    {
        ArrayList<Map<String, String>> rows = null;
        String sqiw  = DB.dbSql(DB.chuliPerfix("imgroupuser"), "gid", "`type`=1 AND `sid`="+aid+"");
        String where = "`type`= 1 AND `id` IN("+sqiw+") AND `gtype` IN(0,1)";
        rows = Model.get(this.nDb, "imgroup").getAll(where, "id,name,face,ushu,bqname,bqcolor", "`id` DESC",0, new CallBack(){
            @Override
            public Map<String, String> backMap(Map<String, String> a) {
                String face = a.get("face");
                if(Rock.isEmpt(face))a.put("face", "images/group.png");
                return super.backMap(a);
            }
        });
        return rows;
    }

    /**
     * 群组信息
     * */
    public Map<String, String> groupInfo(int gid)
    {
        Map<String, String> rs = Model.get(this.nDb,"imgroup").getOne(gid+"", "id,name,explain,ushu,face,bqname,bqcolor,type");
        String face = rs.get("face");
        if(Rock.isEmpt(face))rs.put("face", "images/group.png");
        return rs;
    }
}
